User Post

[Dev] fmt.Println, fmt.Fprintf, fmt.Sprintf in Go Language (Golang)

Last edited by dave on 03/02/2026, 14:48:43 UTC

User:dave / [Dev] fmt.Println, fmt.Fprintf, fmt.Sprintf in Go Language (Golang)

dave

Role: ADMIN

Status: Active

Joined on 24/10/2025, UTC

Contents

When should I use it?

Question 1: “Am I just printing something for a human?”

YES → use fmt.Println

fmt.Println("Hi,", name, age)

Use this for:

  • debugging
  • CLI output
  • quick logs
  • learning

Rule: if you don’t care about exact formatting → Println.

JS equivalent:

console.log("Hi", name, age)

Question 2: “Do I need a formatted string or to write somewhere specific?”

YES → use fmt.Fprintf or fmt.Sprintf

A) Writing somewhere (HTTP, file, buffer)

➡ fmt.Fprintf

fmt.Fprintf(w, "Hi %s, age %d\n", name, age)

Think:

“formatted print to something”

JS equivalent:

res.write(`Hi ${name}, age ${age}`)

B) Need the string value

➡ fmt.Sprintf

msg := fmt.Sprintf("Hi %s, age %d", name, age)

Think:

“string printf”

JS equivalent:

const msg = `Hi ${name}, age ${age}`

TLDR

What you wantUseNewline (\n)
Just printfmt.PrintlnYes
Print formattedfmt.PrintfNo
Write formatted to HTTP/filefmt.FprintfNo
Build a formatted stringfmt.SprintfNo

You can survive your entire Go job knowing only:

  • Println
  • Sprintf
  • Fprintf

Final mental hack (remember this)

P = Print

F = Format

S = String

  • Println → prints
  • Sprintf → string
  • Fprintf → formatted output
Backlinks (0)
  • General

      No backlinks yet.

  • User

      No backlinks yet.

  • Redirects

      No backlinks yet.

  • Media

      No backlinks yet.

  • Categories

      No backlinks yet.

Categories (0)

    No categories assigned to this page.

Edit Level

> User's own page (User:dave and admins only)

Text content on User: pages and its subpages is not licensed under the same terms as regular wiki pages. All rights reserved.

[Dev] fmt.Println, fmt.Fprintf, fmt.Sprintf in Go Language (Golang) by dave | SidWiki